-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[Clang][Basic] Add __has_feature
checks for CFI sanitizers
#151348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: None (moorabbit) ChangesSupport Fixes #151022 Full diff: https://github.com/llvm/llvm-project/pull/151348.diff 3 Files Affected:
diff --git a/clang/include/clang/Basic/Features.def b/clang/include/clang/Basic/Features.def
index 72f23614aef11..4beb9a51dcf0b 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -303,6 +303,7 @@ FEATURE(is_trivially_assignable, LangOpts.CPlusPlus)
FEATURE(is_trivially_constructible, LangOpts.CPlusPlus)
FEATURE(is_trivially_copyable, LangOpts.CPlusPlus)
FEATURE(is_union, LangOpts.CPlusPlus)
+FEATURE(cfi, LangOpts.Sanitize.hasOneOf(SanitizerKind::CFI))
FEATURE(kcfi, LangOpts.Sanitize.has(SanitizerKind::KCFI))
FEATURE(kcfi_arity, LangOpts.Sanitize.has(SanitizerKind::KCFI))
FEATURE(modules, LangOpts.Modules)
diff --git a/clang/include/clang/Basic/Sanitizers.def b/clang/include/clang/Basic/Sanitizers.def
index 1d0e97cc7fb4c..aad758ceb9d40 100644
--- a/clang/include/clang/Basic/Sanitizers.def
+++ b/clang/include/clang/Basic/Sanitizers.def
@@ -134,7 +134,7 @@ SANITIZER("cfi-nvcall", CFINVCall)
SANITIZER("cfi-vcall", CFIVCall)
SANITIZER_GROUP("cfi", CFI,
CFIDerivedCast | CFIICall | CFIMFCall | CFIUnrelatedCast |
- CFINVCall | CFIVCall)
+ CFINVCall | CFIVCall | CFICastStrict)
// Kernel Control Flow Integrity
SANITIZER("kcfi", KCFI)
diff --git a/clang/test/Lexer/has_feature_cfi.c b/clang/test/Lexer/has_feature_cfi.c
new file mode 100644
index 0000000000000..df472944e6d9e
--- /dev/null
+++ b/clang/test/Lexer/has_feature_cfi.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -E -fsanitize=cfi-cast-strict -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
+// RUN: %clang_cc1 -E -fsanitize=cfi-derived-cast -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
+// RUN: %clang_cc1 -E -fsanitize=cfi-icall -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
+// RUN: %clang_cc1 -E -fsanitize=cfi-mfcall -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
+// RUN: %clang_cc1 -E -fsanitize=cfi-unrelated-cast -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
+// RUN: %clang_cc1 -E -fsanitize=cfi-nvcall -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
+// RUN: %clang_cc1 -E -fsanitize=cfi-vcall -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
+// RUN: %clang_cc1 -E %s -o - | FileCheck --check-prefix=CHECK-NO-CFISAN %s
+
+#if __has_feature(cfi)
+int CFISanitizerEnabled();
+#else
+int CFISanitizerDisabled();
+#endif
+
+// CHECK-CFISAN: CFISanitizerEnabled
+// CHECK-NO-CFISAN: CFISanitizerDisabled
+
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking this. Just a few comments from me.
@@ -303,6 +303,7 @@ FEATURE(is_trivially_assignable, LangOpts.CPlusPlus) | |||
FEATURE(is_trivially_constructible, LangOpts.CPlusPlus) | |||
FEATURE(is_trivially_copyable, LangOpts.CPlusPlus) | |||
FEATURE(is_union, LangOpts.CPlusPlus) | |||
FEATURE(cfi, LangOpts.Sanitize.hasOneOf(SanitizerKind::CFI)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll also want to add || LangOpts.Sanitize.hasOneOf(SanitizerKind::LCFI)
into this check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think SanitizerKind::LCFI
exists as a variant of SanitizerKind
. Did you mean SanitizerKind::KCFI
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, my bad.
clang/test/Lexer/has_feature_cfi.c
Outdated
// RUN: %clang_cc1 -E -fsanitize=cfi-unrelated-cast -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s | ||
// RUN: %clang_cc1 -E -fsanitize=cfi-nvcall -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s | ||
// RUN: %clang_cc1 -E -fsanitize=cfi-vcall -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s | ||
// RUN: %clang_cc1 -E %s -o - | FileCheck --check-prefix=CHECK-NO-CFISAN %s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add cases for -fsanitize=cfi
, -fsanitize-cfi-cross-dso
, and their removals with -fno-*
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing that out!
Tried to address this in a new commit.
Let me know if there's any scenario I may have missed and that you would like included.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks
Support `__has_feature(cfi)` to check for control flow integrity sanitizers.
edeef3c
to
d741f65
Compare
My apologies for the force-push. Issue with git. CI was failing with the error |
|
clang/test/Lexer/has_feature_cfi.c
Outdated
// RUN: %clang -E --target=x86_64-linux-gnu -fvisibility=hidden -fno-sanitize-ignorelist -fsanitize=cfi -fno-sanitize-cfi-cross-dso -fno-sanitize=cfi-nvcall,cfi-vcall,cfi-mfcall,cfi-icall -flto -c %s -o - | FileCheck %s --check-prefix=CHECK-CFISAN | ||
|
||
// Disable all CFI schemes. This essentially disables CFI sanitizers. | ||
// RUN: %clang -E --target=x86_64-linux-gnu -fvisibility=hidden -fsanitize=cfi -fno-sanitize-cfi-cross-dso -fno-sanitize=cfi-nvcall,cfi-vcall,cfi-mfcall,cfi-icall,cfi-cast-strict,cfi-derived-cast,cfi-unrelated-cast -flto -c %s -o - | FileCheck %s --check-prefix=CHECK-NO-CFISAN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're testing for x86_64-linux-gnu only, you'll probably want // REQUIRES: x86-registered-target
also
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, since I'm just running the preprocessor (i.e. clang -E
), I probably don't need to set the target at all. I removed the target specification in the new commit. Let me know if you would like me to keep it and require x86-registered-target
.
Here's the complete error message I get locally (CI shows the same):
|
There was also a problem that caused a driver test to fail. I fixed it in b6a7ce7. The issue was that the driver was not expecting After doing a little bit of digging, I found out that the original commit that added the CFI cast sanitizers didn't enable the Ran |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @fmayer
We have been trying to break __has_feature(undefined_behavior_sanitizer)
into fine grained checks (#148310). If we have __has_feature(cfi)
mean CFI || KCFI that is sort of going in the opposite direction. Let's take some time to think about whether it is a good idea to combine them like this.
Also, I just learned that non-standard features are meant to be under
|
Given |
Yes. IMHO, it's a good idea to have a dedicated |
Sounds good to me. |
@moorabbit could you split the check into individual ones akin to #148310 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi -fsanitize-cfi-cross-dso -c %s -o - | FileCheck %s --check-prefix=CHECK-CFI | ||
// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi -fno-sanitize=cfi-nvcall,cfi-vcall,cfi-mfcall,cfi-icall -c %s -o - | FileCheck %s --check-prefix=CHECK-CFI | ||
// RUN: %clang -E -fsanitize=kcfi -c %s -o - | FileCheck %s --check-prefix=CHECK-CFI | ||
// CHECK-CFI: CFISanitizerEnabled |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep kcfi separated for now before we decide to combine them.
// CHECK-KCFI: KCFISanitizerEnabled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree to separate them, but I don't think we can change that later as soon as anyone uses that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Tried to fix it in a new commit.
@@ -303,6 +303,14 @@ FEATURE(is_trivially_assignable, LangOpts.CPlusPlus) | |||
FEATURE(is_trivially_constructible, LangOpts.CPlusPlus) | |||
FEATURE(is_trivially_copyable, LangOpts.CPlusPlus) | |||
FEATURE(is_union, LangOpts.CPlusPlus) | |||
FEATURE(cfi_sanitizer, LangOpts.Sanitize.hasOneOf(SanitizerKind::CFI | SanitizerKind::CFICastStrict | SanitizerKind::KCFI)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FEATURE(cfi_sanitizer, LangOpts.Sanitize.hasOneOf(SanitizerKind::CFI))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in a new commit. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm thanks
small nit: I would say Add or Support rather than Enable in the first line of the commit message
__has_feature(cfi)
__has_feature
checks for CFI sanitizers
Added a |
Yeah, I did the same with the UBSan change. |
Thanks! |
@moorabbit Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Add
__has_feature
checks control flow integrity sanitizers.Fixes #151022